package com.show.web.sys_org;

import com.show.dto.sys_menu.SysMenu;
import com.show.dto.sys_org.SysOrg;
import com.show.dto.sys_role.Ztree;
import com.show.filter.LoginController;
import com.show.service.org.SysOrgService;
import com.show.util.HelperClazz;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * Created by 顾志杰 on 2018/2/2.
 */
@Controller
@RequestMapping(value = "/org")
public class SysOrgController {
    @Autowired
    private SysOrgService orgService;
    @Autowired
    private LoginController loginController;
    @GetMapping("/header")
    @ApiOperation(value = "跳转RoleInsert", notes = "")
    public String header(Map<String, Object> map, Principal principal) throws Exception {
//        CgEmp cgEmp = cgEmpService.getEmp(principal.getName());
//        map.put("emp", cgEmp);
        return "header";
    }
    @GetMapping("toSysOrg")
    @ApiOperation(value = "跳转跳转部门页主页", notes = "")
    public String toSysOrg(Map<String, Object> map, HttpServletRequest request) throws Exception {
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'","");
        List<SysMenu> list = loginController.getMenus(userName);
        String empName = loginController.getEmp(userName);
        map.put("list",list);
        map.put("empName",empName);
        return "depart_manage/depart_index";
    }
    @GetMapping("toSysOrgList")
    @ApiOperation(value = "跳转部门页", notes = "")
    public String toSysOrgList(Map<String, Object> map) throws Exception {
        List<Ztree> list = orgService.OrgTree();
        map.put("zTree", list);
        return "depart_manage/depart_list";
    }
    @ApiOperation(value="按ID查询部门", notes="按ID查询部门")
    @GetMapping("/getOrg/{orgId}")
    @ResponseBody
    @ApiImplicitParam(name = "orgId", paramType = "path", value = "部门id", dataType = "String")
    public SysOrg getOrg(@PathVariable Integer orgId, HttpServletResponse response){
        SysOrg org = orgService.FindOneOrg(orgId);//按Id查询部门
        return org;
    }
    @ApiOperation(value="修改部门", notes="修改部门")
    @PostMapping("/updateOrg")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "orgName", paramType = "query", value = "部门名称", dataType = "String")
            , @ApiImplicitParam(name = "parentOrgId", paramType = "query", value = "父级部门id", dataType = "Integer")
            , @ApiImplicitParam(name = "parentOrgName", paramType = "query", value = "父级部门名称", dataType = "String")
            , @ApiImplicitParam(name = "orgId", paramType = "query", value = "父级部门名称", dataType = "Integer")
    })
    public String updateOrg(SysOrg sysOrg){
        return orgService.UpdOrg(sysOrg);
    }
    @ApiOperation(value="按ID删除部门", notes="按ID删除部门")
    @PostMapping("/deleteOrg/{orgId}")
    @ResponseBody
    @ApiImplicitParam(name = "orgId", paramType = "path", value = "部门id", dataType = "String")
    public String deleteOrg(@PathVariable Integer orgId){
        return orgService.DeleteOrg(orgId);
    }
    @ApiOperation(value="按ID查询部门", notes="按ID查询部门")
    @PostMapping("/saveOrg")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "orgName", paramType = "query", value = "部门名称", dataType = "String")
            , @ApiImplicitParam(name = "parentOrgId", paramType = "query", value = "父级部门id", dataType = "Integer")
            , @ApiImplicitParam(name = "parentOrgName", paramType = "query", value = "父级部门名称", dataType = "String")
    })
    public String saveOrg(SysOrg sysOrg){
        return orgService.InsertOrg(sysOrg);
    }
}
